import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import ParkingMange from '../views/parkingMange/index'
import CarManage from '../views/carManage/index'
import ReserveManage from '../views/reserveManage/index'
import EquipmentManage from '../views/equipmentManage/index'
import NoticeManage from '../views/noticeManage/index'
import SystemManage from '../views/systemManage'
import UserManage from '../views/userManage/index'
import Login from '../views/login'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    component: Home,
    redirect: '/parkingMange',
    children: [
      {
        path: '/parkingMange',
        name: 'parkingMange',
        component: ParkingMange
      },
      {
        path: '/systemManage',
        name: 'systemManage',
        component: SystemManage
      },
      {
        path: '/noticeManage',
        name: 'noticeManage',
        component: NoticeManage
      },
      {
        path: '/equipmentManage',
        name: 'equipmentManage',
        component: EquipmentManage
      },
      {
        path: '/reserveManage',
        name: 'reserveManage',
        component: ReserveManage
      },
      {
        path: '/carManage',
        name: 'carManage',
        component: CarManage
      },
      {
        path: '/userManage',
        name: 'userManage',
        component: UserManage
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
router.beforeEach((to, from, next) => {
  const adminId = store.state.adminId
  if (to.path === '/login') {
    next()
  } else {
    if (adminId) {
      next()
    } else {
      next({
        name: 'login',
        query: { redirect: to.fullPath }
      })
    }
  }
})
export default router
